<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/class.ovfLista.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/class.ovfForm.php';

$objSess->setReqSV('rcom', 'asez', 'referent');
$objSess->setReqSV('rcom', 'idAnag', 0);
$idAnag = intval($objSess->getSV('rcom', 'idAnag'));

$codSez = $objSess->getSV('rcom','asez');
$oLista = new ovfLista($codSez,'default');

$aWhere = array(" s.id_anag = ".$idAnag);
$sOrder = '';
$sX = '(';
foreach($oLista->aCampiList as $kX => $aX) {
	if (substr($aX['campo'],0,2)!='S_') continue;
	if ($sOrder == '') {
		$sOrder = " ORDER BY s.".substr($aX['campo'],2);
	}
	$sX .= "s.".substr($aX['campo'],2)." LIKE '%".addslashes(reqVar('pagifilt'))."%' OR ";
}
if (reqVar('pagifilt')!='' && $sX != '(') {
	$aWhere[] = substr($sX,0,-4).')';
}
$sWhere = (count($aWhere)==0)? "":" WHERE ".implode(" AND ",$aWhere);
$sSql = "SELECT count(*) AS num_rec " .
		" FROM ovf_sez_$codSez s " .$sWhere;
//dbg_($sSql);
$objDB->query($sSql,'selAsezId');
$acRec = $objDB->fetch('selAsezId');
$numRec = $acRec['num_rec'];
$link = $_SERVER['PHP_SELF']."?".$sXSNQS;
$startOffset = 0;
$endOffset = 0;
$sFiltro = ovfGetFiltro($link);
$sPaginaz = ovfGetPaginaz($link, $numRec, $startOffset, $endOffset);

$sSql = "SELECT s.id_$codSez " .
		" FROM ovf_sez_$codSez s " .$sWhere.$sOrder.
		" LIMIT ".$startOffset.",".($endOffset-$startOffset);
//dbg_($sSql);
$objDB->query($sSql,'selAsezId');
$cmdL = "";
$cmdL .= "<a class=\"cmd1\" href=\"asezListPdf.php?$sXSNQS\" target=\"_blank\">"
	."<img src='".OVF_URL_ROOT."/app/_img/ico_pdf.gif' border='0'> pdf</a>\n";
$cmdR = "";
$cmdR .= "<a href='asezIns.php?$sXSNQS' class='cmd1'>"._t('nuova registrazione')."</a>";
$cmdR .= cmdTorna(); 

// testata sezione anagrafica
$oFormTestaSez = new ovfForm('testasez');
//dbg_($oForm);
$sSql = "SELECT ".$oFormTestaSez->getCampiSelect()
	." FROM ovf_anagrafiche A WHERE A.id_anag = ".$idAnag;
$objDB->query($sSql);
if (!$oFormTestaSez->aCampiDB = $objDB->fetch()) {
	backOnErr("id. errato '".$idAnag."'");
}
$oFormTestaSez->db2form();

$sHtmlAnag = "<table class=\"tab1\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" align='center'>";
$sHtmlAnag .= getHtmlRigaRif();
$sHtmlAnag .= $oFormTestaSez->getHtmlCampiView();
$sHtmlAnag .= "</table>";

$aOpz['sottotit'] = $sHtmlAnag;

testaHtml(_t('Anagrafe'),_t('Lista').' '.$oLista->descr, $msgPag, 'rcom', $aOpz);
echo getHtmlForm();
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('asez');
echo getHtmlHidden('startoffset');
?>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right">
         <?=$cmdR?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
			<?=$sFiltro?>
      </td>
      <td class="cmd1" align="right">
			<?=$sPaginaz?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<? echo getHtmlRigaRif(20);?>
   <tr>
      <?php echo $oLista->getHtmlLab();?>
   </tr>
<? 
$clRF = 'txt2';
while($acZ = $objDB->fetch('selAsezId')) { 
	$clRF = ($clRF=='txt1')?'txt2':'txt1';	
	$sSql = "SELECT ".$oLista->getCampiSelect('S')
		." FROM ovf_sez_$codSez S "
		." WHERE S.id_$codSez = ".$acZ["id_$codSez"];
	$acX = $objDB->query($sSql,'selAsezIdX');
	$acX = $objDB->fetch('selAsezIdX');
	$aOpt1 = array("class"=>$clRF);
	$aOpt1['href'] = "asezView.php?id=".$acZ["id_$codSez"]."&$sXSNQS";
	echo "<tr>\n";
	echo $oLista->getHtmlDat($acX,$aOpt1);
   echo "</tr>\n";
 } ?>
</table>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right" colspan='2'>
<?=$sPaginaz?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right">
         <?=$cmdR?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
?>
